home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / pas_all.zip / TI159.ASC < prev    next >
Text File  |  1991-09-11  |  6KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  10.   VERSION : 2.0, 3.0
  11.        OS : PC-DOS, MS-DOS, CP/M-86
  12.      DATE : May 21, 1986                                 PAGE : 1/4
  13.     TITLE : REAL FORMAT CONVERSION
  14.  
  15.  
  16.  
  17.  
  18.   The following programs may be used to convert real number data
  19.   format from that used by Turbo Pascal with 8087 support, to the
  20.   format used by regular Turbo Pascal, and vice versa. The first
  21.   program converts from 8087 to regular, and the second program
  22.   converts from regular to 8087.
  23.  
  24.   Program IEEErealTOregular;
  25.   {  Converts an 8-byte,  IEEE real to Turbo  Pascal's  6-byte real format. }
  26.   type
  27.     ieee       = array[1..8] of byte;
  28.     turbo_real = array[1..6] of byte;
  29.     mant       = array[1..5] of byte;
  30.     rf         = file of ieee;
  31.  
  32.   var
  33.     f : rf;
  34.  
  35.   procedure IEEEtoTurbo(long : ieee; var r : real);
  36.        { Convert from IEEE to Turbo Pascal's 6-byte real format }
  37.   var
  38.     i : integer;
  39.     e : byte;
  40.     t : turbo_real absolute r;
  41.     sign : byte;
  42.  
  43.   begin
  44.     { initialize variables }
  45.  
  46.     r := 0.0;
  47.     for i := 2 to 5 do
  48.       t[i] := 0;
  49.  
  50.     i := (long[8] and $7f) shl 4;       { get 7 highest bits of exponent }
  51.     i := i or ((long[7] and $f0) shr 4);{ get rest of exponent }
  52.  
  53.     if (i < 985) or (i > 1061) then     { check to make sure exponent }
  54.     begin                               { is in legal range }
  55.       writeln('exponent too large');
  56.       exit;
  57.     end;
  58.  
  59.     i := i - 1023;                         { take out bias }
  60.     t[1] := i + $81;                       { put in new bias }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  76.   VERSION : 2.0, 3.0
  77.        OS : PC-DOS, MS-DOS, CP/M-86
  78.      DATE : May 21, 1986                                 PAGE : 2/4
  79.     TITLE : REAL FORMAT CONVERSION
  80.  
  81.  
  82.  
  83.  
  84.     sign := long[8] and $80;               { get sign bit }
  85.  
  86.        { build up most significant byte }
  87.  
  88.     t[6] := sign + ((long[7] and $0f) shl 3) or ((long[6] and $e0) shr 5);
  89.  
  90.     {make rest of real number in groups of 5 and 3 }
  91.  
  92.     for i := 5 downto 2 do
  93.       t[i] := ((long[i+1] and $1f) shl 3) or ((long[i] and $e0) shr 5);
  94.  
  95.   end;                                  { IEEEtoTurbo }
  96.  
  97.   var
  98.     t : ieee;
  99.     r : real;
  100.  
  101.   begin                                 { program }
  102.     assign(f, 'real87.dat');            { open input file }
  103.     reset(f);
  104.     while not eof(f) do
  105.     begin
  106.       read(f, t);                       { read an 8-byte real }
  107.       ieeetoturbo(t,  r);               { convert to a 6-byte real }
  108.       writeln(r);                       { display results }
  109.     end;
  110.   end.                                  { program }
  111.  
  112.  
  113.   program RegularTO87real;
  114.        { Converts a 6-byte real to an 8-byte, IEEE real.  }
  115.   type
  116.     ieee       = array[1..8] of byte;
  117.     turbo_real = array[1..6] of byte;
  118.     rf         = file of ieee;
  119.  
  120.   var
  121.     f : rf;
  122.     i : file of real;
  123.  
  124.   procedure TurboToIEEE(r : real; var long : ieee);
  125.        {  Converts from a 6-byte Turbo Pascal real number to an  8-
  126.           byte, IEEE format. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  142.   VERSION : 2.0, 3.0
  143.        OS : PC-DOS, MS-DOS, CP/M-86
  144.      DATE : May 21, 1986                                 PAGE : 3/4
  145.     TITLE : REAL FORMAT CONVERSION
  146.  
  147.  
  148.  
  149.  
  150.   var
  151.     realthing : turbo_real absolute r;
  152.     exp : integer;
  153.  
  154.     sign, temp : byte;
  155.     i    : integer;
  156.  
  157.   begin
  158.     fillchar(long, sizeof(long), 0);
  159.  
  160.     { build up exponent and sign of long real }
  161.  
  162.     sign := realthing[6] and $80;        { get sign bit from 6-byte }
  163.     exp := realthing[1] - $81;           { get exponent & take out bias}
  164.     exp := (exp + 1023) shl 4;           { re-bias && left justify }
  165.     exp := exp or (sign shl 8);          { put in sign }
  166.     long[8] := hi(exp);                  { insert into long real }
  167.     long[7] := lo(exp);
  168.  
  169.     { make mantissa }
  170.  
  171.     for i := 6 downto 3 do
  172.     begin
  173.  
  174.       { build up a byte }
  175.       temp := (realthing[i] and $7f shl 1) or
  176.               (realthing[i - 1] and $80 shr 7);
  177.  
  178.       { split byte and insert into long real }
  179.       long[i+1] := long[i+1] + temp and $f0 shr 4;
  180.       long[i] := long[i] + temp and $f shl 4;
  181.  
  182.     end;
  183.  
  184.     { take care of last (incomplete) byte }
  185.     temp := realthing[2] and $7f shl 1;
  186.     long[3] := long[3] + temp and $f0 shr 4;
  187.     long[2] := long[2] + temp and $f shl 4;
  188.  
  189.   end; { TurboToIEEE}
  190.   var
  191.     t : ieee;
  192.     r : real;
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  208.   VERSION : 2.0, 3.0
  209.        OS : PC-DOS, MS-DOS, CP/M-86
  210.      DATE : May 21, 1986                                 PAGE : 4/4
  211.     TITLE : REAL FORMAT CONVERSION
  212.  
  213.  
  214.  
  215.  
  216.     fname : string[80];
  217.  
  218.  
  219.   begin                              { program }
  220.     write('input file name? ');      { open input file }
  221.     readln(fname);
  222.     assign(i, fname);
  223.     reset(i);
  224.     fname := '';
  225.     write('output file name? ');     { open output file }
  226.     readln(fname);
  227.     assign(f, fname);
  228.     rewrite(f);
  229.     while not eof(i) do
  230.     begin
  231.       read(i, r);                    { read a 6-byte real }
  232.       TurboToIEEE(r, t);             { convert to an 8-byte real }
  233.       write(f, t);                   { output result to file }
  234.     end;
  235.     close(f);
  236.   end.       {program}               { close output file }
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.